package com.abe.bathsystem.contract.presenter;

import com.abe.bathsystem.android.v3.MainBathAbsActivity;
import com.abe.bathsystem.contract.SerialPortContract;
import com.abe.bathsystem.contract.SerialPortModel;
import com.abe.bathsystem.entity.serial.SerialDataEntity;
import com.abe.bathsystem.utils.SerialPortParse;
import com.abe.libcore.utils.normal.CommonUtils;
import com.abe.libcore.utils.normal.ConstantCore;
import com.abe.mvpcore.BasePresenter;
import com.google.gson.Gson;

public class SerialPortPresenter extends BasePresenter<SerialPortContract.SerialPortView, SerialPortModel> implements SerialPortContract.SerialPortPresenter, SerialPortContract.SerialPortPresenter.ISerialPortBack {

    public SerialPortPresenter() {
    }

    @Override
    public SerialPortModel getModel() {
        return SerialPortModel.from(this);
    }

    @Override
    public void openSerialPort(int type) {
        getModel().openSerialPort(type);
    }

    @Override
    public void sendCommand(byte[] buffer, boolean isTest) {
        getModel().sendCommand(buffer, isTest);
    }

    @Override
    public void closeSerialPort() {
        getModel().closeSerialPort();
    }

    @Override
    public void onResult(int code, SerialDataEntity data, String errorMSG) {
        StringBuilder builder = new StringBuilder();
        String newLine = "\r\n";
        builder.append("================================================================================================").append(newLine);
        String action = "操作返回";
        switch (code) {
            case SerialPortModel.CODE_OPEN_SUCCESS:
                action = "串口开启命令" + action;
                getView().onConnectSuccess();
                break;
            case SerialPortModel.CODE_CLOSE_SUCCESS:
                action = "串口关闭命令" + action;
                getView().onCloseSuccess();
                break;
            case SerialPortModel.CODE_SEND_DATA_SUCCESS:
                action = "串口发送命令成功";
                getView().onSendDataSuccess();
                break;
            case SerialPortModel.CODE_RECEIVE_DATA_SUCCESS:
                action = "串口接收数据返回成功";
                String type = SerialPortParse.parse(data);
                getView().onReceiveData(!type.equals(SerialPortParse.PARSE_ERROR), type, data);
                break;
            case SerialPortModel.CODE_RECEIVE_DATA_FAILED:
                action = "串口数据接收失败";
                getView().onReceiveData(false, SerialPortParse.PARSE_ERROR, null);
                break;
            case SerialPortModel.CODE_ERROR:
                action = "串口错误:" + errorMSG;
                getView().onError(errorMSG);
                break;
            default:
                break;
        }
        builder.append("Serial:onResult**********action:").append(action).append(newLine);
        builder.append("Serial:onResult:*********Data:").append(new Gson().toJson(data)).append(newLine);
        CommonUtils.saveLocalDailyLog(MainBathAbsActivity.getRealTime(), builder.toString(), ConstantCore.LOG_SERIAL_PATH);
    }
}
